global class BatchInsertFraudOpportunityData implements Database.Batchable<sObject> {
global string query;
	global BatchInsertFraudOpportunityData() {
		List<Account> tempAcc = [Select a.OppyIdWithLowestStatusValue__c
        				 From Account a
        				 where a.RecordType.Name = 'West - Student Account' and a.OppyIdWithLowestStatusValue__c!=null];
        system.debug('tempAcc 11');
        system.debug(tempAcc.size());
        Set<Id> oppId = new Set<Id>();
        string chk = '\'';
	    integer i = 0;
        for(Account temp: tempAcc) {
        	oppId.add(temp.OppyIdWithLowestStatusValue__c);
        	chk = chk +''+ temp.OppyIdWithLowestStatusValue__c + '\'';
	   		i++;	
	   		if(i<tempAcc.size())
	   			chk = chk + ' or o.id = \'';
        	
        }
        
        
		query = 'Select o.Account.FirstName,o.Account.Middle_Name__pc, o.Account.LastName,'+
                       'o.Application_ID__c,o.Lead_Creation_Date__c,o.Account.PersonHomePhone,'+
                       'o.Account.PersonOtherPhone,o.Account.Work_Phone__pc,o.Account.Other_Phone_2__pc,'+
                       'o.Account.PersonMobilePhone,o.Account.Fax,o.Account.PersonMailingStreet,'+
                       'o.Account.PersonMailingCity,o.Account.PersonMailingState,o.Account.PersonMailingPostalCode,'+
                       'o.Account.PersonOtherStreet,o.Account.PersonOtherCity,o.Account.PersonOtherState,'+
                       'o.Account.PersonOtherPostalCode,o.Account.Personal_Email__pc,o.Account.Other_Email__pc,o.Account.Work_Email__pc,'+
                       'o.Account.Other_Email_2__pc,o.Account.University_Email__pc,o.Program__r.External_ID_1__c,o.Campus__r.External_ID_2__c,'+
                       'o.Fraud_Enrollment_Status__c,o.LeadSource,o.Anticipated_Start_Date__c,o.Start_Date__c,'+
                       'o.Account.PersonBirthDate,o.Fraud_Flag__c, o.Status_Fraud_Value__c,o.StageName,o.Account.Account_ID__c' +                                            
                 ' From Opportunity o' +
                 ' where o.RecordType.Name = \'West - Student Application\'' +
                 ' and o.Lead_Creation_Date__c = LAST_N_DAYS:365 and (o.id = '+chk + ')';
	}
	
	global Database.Querylocator start(Database.BatchableContext BC) {
		return Database.getQueryLocator(query);
	}
	
	global void execute(Database.BatchableContext BC,List<Opportunity> insertFraudOpp) {
		
		setFraudDataOpp(insertFraudOpp);
	}
	
	global void finish(Database.BatchableContext BC) {
		
	}
	
	private void setFraudDataOpp(List<Opportunity> lstFraudOpp) {
        List<Fraud_Report_Data__c> lstFraudReportDataOpportunity = new List<Fraud_Report_Data__c>();
        
        for(Opportunity o : lstFraudOpp){
            Fraud_Report_Data__c tempFF = new Fraud_Report_Data__c();
            tempFF.First_Name__c = o.Account.FirstName;
            tempFF.Middle_Name__c = o.Account.Middle_Name__pc;
            tempFF.Last_Name__c = o.Account.LastName;
            tempFF.IRN__c = o.Application_ID__c;
            tempFF.Lead_Create_Date__c = o.Lead_Creation_Date__c.format('MM/dd/yyyy');
            tempFF.Home_Phone__c = o.Account.PersonHomePhone;
            tempFF.Home2_Phone__c = o.Account.PersonOtherPhone;
            tempFF.Business_Phone__c = o.Account.Work_Phone__pc;
            tempFF.Business2_Phone__c = o.Account.Other_Phone_2__pc;
            tempFF.Mobile_Phone__c = o.Account.PersonMobilePhone;
            tempFF.Home_Fax_Phone__c = o.Account.Fax;
            tempFF.Home_Address_Line_1__c = o.Account.PersonMailingStreet;
            tempFF.Home_City__c = o.Account.PersonMailingCity;
            tempFF.Home_State__c = o.Account.PersonMailingState;
            tempFF.Home_Zip__c = o.Account.PersonMailingPostalCode;
            tempFF.Business_Address_Line_1__c = o.Account.PersonOtherStreet;
            tempFF.Business_City__c = o.Account.PersonOtherCity;
            tempFF.Business_State__c = o.Account.PersonOtherState;
            tempFF.Business_Zip__c = o.Account.PersonOtherPostalCode;
            tempFF.Home_Email__c = o.Account.Personal_Email__pc;
            tempFF.Home2_Email__c = o.Account.Other_Email__pc;
            tempFF.Business_Email__c = o.Account.Work_Email__pc;
            tempFF.Business2_Email__c = o.Account.Other_Email_2__pc;
            tempFF.Student_Email__c = o.Account.University_Email__pc;
            tempFF.Program_F__c = o.Program__r.External_ID_1__c;
            tempFF.Home_Campus__c = o.Campus__r.External_ID_2__c;
            tempFF.Enrollment_Status__c = o.Fraud_Enrollment_Status__c;
            tempFF.Primary_Lead_Source__c = o.LeadSource;
            tempFF.Anticipated_Start_Date__c = o.Anticipated_Start_Date__c!=null?o.Anticipated_Start_Date__c.format():'';
            tempFF.Actual_Start_Date__c = o.Start_Date__c!=null?o.Start_Date__c.format():'';
            tempFF.Birth_Date__c = o.Account.PersonBirthDate!=null?o.Account.PersonBirthDate.format():'';
            tempFF.Fraud_Flag__c = o.Fraud_Flag__c;
            lstFraudReportDataOpportunity.add(tempFF);   
        }
        insert lstFraudReportDataOpportunity;
    }
}